package com.macllvm.whale.utils;

import java.net.InetAddress;
import java.net.SocketException;
import java.net.NetworkInterface;
import java.util.Enumeration;
import android.util.Log;

public class NetworkTools {
    private static final String TAG = "NetworkTools";
    public static String getLocalIP() {
        String ip = "";
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            for (; en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                for (; enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                        ip = inetAddress.getHostAddress().toString();
                        break;
                    }
                }
            }
        } catch (SocketException e) {
            Log.e(TAG, "can not get local ip");
            e.printStackTrace();
        }

        return ip;
    }
}